package com.dmz.czykt.util;

import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.ftp.Ftp;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.net.ftp.FTP;

import java.io.BufferedInputStream;
import java.io.File;
import java.util.Properties;

/**
 * @author lee
 */
@Slf4j
public class CzyktSftpFileUtil {


    /**
     * 使用ESAPI增强的上传方法
     */
    public boolean ConnectUpload(String user, String pass, String ip, int port,
                                 String uploadFile, String fileName) {
        boolean result = true;


        ChannelSftp sftp = null;
        Session session = null;
        Channel channel = null;
        JSch jsch = new JSch();
        try {
            session = jsch.getSession(user, ip, port);
            session.setPassword(pass);
            Properties config = new Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
            session.connect(30000);
            channel = session.openChannel("sftp");
            channel.connect(1000);
            sftp = (ChannelSftp) channel;
            log.info("connect:OK！");
        } catch (Exception e) {
            log.error("error:", e);
            result = false;
        }

        if (result) {
            try {
                File srcFile = FileUtil.newFile(fileName);

                BufferedInputStream filein = FileUtil.getInputStream(srcFile);
                sftp.cd(uploadFile);//进入上传地址

                System.out.println(srcFile.getName());
                sftp.put(filein, srcFile.getName());
                System.out.println("upload:ok");
                log.info("upload:ok");
            } catch (Exception e) {
                e.printStackTrace();
                log.error("error:" + e);
                result = true;
            } finally {
                session.disconnect();
                channel.disconnect();
                sftp.disconnect();
            }
        }
        return result;
    }

    /**
     * 使用ESAPI增强的下载方法
     */
    public boolean ConnectDownload(String user, String pass, String ip, int port,
                                   String downloadFile, String fileName, String saveFile) {
        boolean result = true;

        ChannelSftp sftp = null;
        Session session = null;
        Channel channel = null;
        JSch jsch = new JSch();
        try {
            session = jsch.getSession(user, ip, port);
            session.setPassword(pass);
            Properties config = new Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
            session.connect(3000);
            channel = session.openChannel("sftp");
            channel.connect(10000);
            sftp = (ChannelSftp) channel;
            log.info(" connect:OK！");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result = false;
        }

        if (result) {
            try {
                sftp.cd(downloadFile);//进入下载地址
                sftp.get(fileName, saveFile);//目标文件和保存地址
                System.out.println("ok了？");
            } catch (Exception e) {
                System.out.println("没有文件：" + e);
                log.error("SFTP下载失败:{}", String.valueOf(e));
                result = false;
            } finally {
                session.disconnect();
                channel.disconnect();
                sftp.disconnect();
            }
        }
        return result;
    }

    public static boolean uploadFile(String sFileName, String bpsftpuser, String bpsftpPasswd,
                                     String bpsftpIP, int bpsftpPort, String bpsftpDir) {


        try {
            log.info("uploadFile:: sFileName::{}", sFileName);
            Ftp ftp = new Ftp(bpsftpIP, bpsftpPort, bpsftpuser, bpsftpPasswd);
            System.out.println("用户名:" + bpsftpuser + "---密码:" + bpsftpPasswd + "---ip:" + bpsftpIP + "---端口:" + bpsftpPort + "---上传地址:" + bpsftpDir);
            ftp.init();
            ftp.getClient().setFileType(FTP.BINARY_FILE_TYPE);
            File sourceFile = FileUtil.newFile(sFileName);
            ftp.upload(bpsftpDir, sourceFile);
            log.info("uploadok");
            return true;

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return false;
        }
    }

    public static boolean DownFile(String fileName, String bpsftpuser, String bpsftpPasswd,
                                   String bpsIp, int bpsPort, String localpath, String bpsftpDown) {


        try {
            Ftp ftp = new Ftp(bpsIp, bpsPort, bpsftpuser, bpsftpPasswd);
            ftp.init();
            ftp.getClient().setFileType(FTP.BINARY_FILE_TYPE);
            ftp.download(bpsftpDown, fileName, FileUtil.newFile(localpath + fileName));
            return true;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return false;
        }
    }
}